package com.example.mytest6;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.example.mytest6.Test.DAO_CarInfo;
import com.example.mytest6.Test.GetCarInfoApi;
import com.example.mytest6.Test.GetCarInfoBean;
import com.example.mytest6.Test.GetCarInfoBean_json;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener, View.OnClickListener {
    public static final int PAGE_ONE = 0;
    public static final int PAGE_TWO = 1;
    public static final int PAGE_THREE = 2;
    public static final int PAGE_FOUR = 3;
    public static final int PAGE_FIVE = 4;
    public static final int PAGE_SIX = 5;
    public static final int PAGE_SEVEN = 6;
    private ViewPager vpager;
    private MyFragmentPagerAdapter mAdapter;
    private RadioButton fg0;
    private RadioButton fg1;
    private RadioButton fg2;
    private RadioButton fg3;
    private RadioButton fg4;
    private RadioButton fg5;
    private RadioButton fg6;
    private RadioGroup rg_tab_bar;
    private String TAG = "MainActivity";
    private DaoMaster daoMaster;
    private DaoMaster.DevOpenHelper carInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
        initView();
        fg0.setChecked(true);
    }

    private void initView() {
        fg0 = findViewById(R.id.fg0);
        fg1 = findViewById(R.id.fg1);
        fg2 = findViewById(R.id.fg2);
        fg3 = findViewById(R.id.fg3);
        fg4 = findViewById(R.id.fg4);
        fg5 = findViewById(R.id.fg5);
        fg6 = findViewById(R.id.fg6);
        rg_tab_bar = findViewById(R.id.rg_tab_bar);

        vpager = findViewById(R.id.vpager);
        vpager.setAdapter(mAdapter);

        vpager.setCurrentItem(0);
        vpager.addOnPageChangeListener(this);
        fg0.setOnClickListener(this);
        fg1.setOnClickListener(this);
        fg2.setOnClickListener(this);
        fg3.setOnClickListener(this);
        fg4.setOnClickListener(this);
        fg5.setOnClickListener(this);
        fg6.setOnClickListener(this);

//        initLoad();
//        initCarInfo();
//        carInfo = new DaoMaster.DevOpenHelper(this, "CarInfo", null);
//        daoMaster = new DaoMaster(carInfo.getWritableDatabase());
//        DAO_CarInfoDao dao_carInfoDao = daoMaster.newSession().getDAO_CarInfoDao();
//        dao_carInfoDao.insert(new DAO_CarInfo(null, "王生安", "370101196101011001", "男", "user1", "13804110001", "1990-05-21 14:19:21", "user1@qq.com"));
//        int size = dao_carInfoDao.queryBuilder().list().size();
//        Toast.makeText(this, size + "", Toast.LENGTH_SHORT).show();
    }

    //点击切换事件
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.fg0:
                vpager.setCurrentItem(0);
                break;
            case R.id.fg1:
                vpager.setCurrentItem(1);
                break;
            case R.id.fg2:
                vpager.setCurrentItem(2);
                break;
            case R.id.fg3:
                vpager.setCurrentItem(3);
                break;
            case R.id.fg4:
                vpager.setCurrentItem(4);
                break;
            case R.id.fg5:
                vpager.setCurrentItem(5);
                break;
            case R.id.fg6:
                vpager.setCurrentItem(6);
                break;
        }
    }

    //页面viewPpager 页面的切换方法
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {
        if (state == 2) {
            switch (vpager.getCurrentItem()) {
                case PAGE_ONE:
                    fg0.setChecked(true);
                    break;
                case PAGE_TWO:
                    fg1.setChecked(true);
                    break;
                case PAGE_THREE:
                    fg2.setChecked(true);
                    break;
                case PAGE_FOUR:
                    fg3.setChecked(true);
                    break;
                case PAGE_FIVE:
                    fg4.setChecked(true);
                    break;
                case PAGE_SIX:
                    fg5.setChecked(true);
                    break;
                case PAGE_SEVEN:
                    fg6.setChecked(true);
                    break;
            }
        }

    }

    //adapter类
    public class MyFragmentPagerAdapter extends FragmentPagerAdapter {

        private final int PAGER_COUNT = 7;
        private MyFragment1 myFragment1 = null;
        private MyFragment2 myFragment2 = null;
        private MyFragment3 myFragment3 = null;
        private MyFragment4 myFragment4 = null;
        private MyFragment5 myFragment5 = null;
        private MyFragment6 myFragment6 = null;
        private MyFragment7 myFragment7 = null;

        public MyFragmentPagerAdapter(FragmentManager fm) {
            super(fm);
            myFragment1 = new MyFragment1();
            myFragment2 = new MyFragment2();
            myFragment3 = new MyFragment3();
            myFragment4 = new MyFragment4();
            myFragment5 = new MyFragment5();
            myFragment6 = new MyFragment6();
            myFragment7 = new MyFragment7();
        }

        @Override
        public Fragment getItem(int position) {
            Fragment fragment = null;
            switch (position) {
                case MainActivity.PAGE_ONE:
                    fragment = myFragment1;
                    break;
                case MainActivity.PAGE_TWO:
                    fragment = myFragment2;
                    break;
                case MainActivity.PAGE_THREE:
                    fragment = myFragment3;
                    break;
                case MainActivity.PAGE_FOUR:
                    fragment = myFragment4;
                    break;
                case MainActivity.PAGE_FIVE:
                    fragment = myFragment5;
                    break;
                case MainActivity.PAGE_SIX:
                    fragment = myFragment6;
                    break;
                case MainActivity.PAGE_SEVEN:
                    fragment = myFragment7;
                    break;
            }
            return fragment;
        }

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            return super.instantiateItem(container, position);
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            System.out.println("destroy" + position);
            super.destroyItem(container, position, object);
        }

        @Override
        public int getCount() {
            return PAGER_COUNT;
        }


    }

    @Override
    protected void onStop() {
        super.onStop();
        daoMaster.newSession().clear();
        carInfo.close();
        Log.e(TAG, "onStop: " + "====");
    }
}
